package com.software.levelupandroid.utils;

import android.content.Context;
import android.util.TimeUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @Description OkHttp的工具类
 * @Author LiangYaJie
 * @CreateTime 2024年05月14日 10:07:24
 */
public class OkhttpUtil {

    private static final String BASE_URL0 = "http://192.168.56.1:8080/levelup/";
    private static final String BASE_URL1 = "http://192.168.43.163:8080/levelup/";
    private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build();

    /**
     * post请求
     * @param requestBody
     * @param url
     * @return
     */
    public static Call postRequest(RequestBody requestBody, String url, Context context){
        Request request =  new Request.Builder()
                .post(requestBody)
                .header("Authorization", "level-up " + TokenUtil.getToken(context)) //添加token
                .url(BASE_URL0 + url)
                .build();

        return okHttpClient.newCall(request);
    }

    /**
     * get请求
     * @param url
     * @return
     */
    public static Call getRequest(String url, Context context){
        Request request =  new Request.Builder()
                .get()
                .header("Authorization", "level-up " + TokenUtil.getToken(context)) //添加token
                .url(BASE_URL0 + url)
                .build();
        return okHttpClient.newCall(request);
    }

    /**
     * 获取OkHttpClient对象
     * @return
     */
    public static OkHttpClient getOkHttpClient(){
        return okHttpClient;
    }

}
